import 'package:mvvm_provider/base/base_state.dart';
import 'package:mvvm_provider/config/handle_state.dart';
import 'package:mvvm_provider/page/mine/data/model/info_model.dart';
import 'package:mvvm_provider/page/mine/data/repository/mine_repository.dart';
import 'package:mvvm_provider/page/mine/states/mine_state.dart';
import '../../../base/base_view_model.dart';
import '../../../model/response_model.dart';

class MineViewModel extends BaseViewModel<MineState> {
  MineViewModel() : super(MineState());

  /// 获取数据
  Future<void> getListData(bool isRefresh) async {
    int page = state.page;
    if (isRefresh) {
      page = 1;
    } else {
      page++;
    }
    ResponseModel responseModel = await MineRepository.getInfoListData<InfoListModel>(page);

    /// 处理页面状态
    state.netState = HandleState.handle(responseModel);
    if (state.netState == NetState.dataSuccessState) {
      /// 数据请求成功
      InfoListModel infoListModel = responseModel.data;
      if (page == 1) {
        state.dataList = infoListModel.works;
        if (state.dataList.isEmpty) {
          state.netState = NetState.emptyDataState;
        }
      } else {
        state.dataList.addAll(infoListModel.works);

        /// 显示没有更多数据了
        if (infoListModel.works.isEmpty) {
          refreshController.loadNoData();
        }
      }
      state.page = page;
      refreshController.refreshCompleted();
      refreshController.loadComplete();
    }
    notifyListeners();
  }
}
